#include "student.h"
#include "../Include/log_pub.h"
#include "../Include/data_input.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// 初始化学生链表
void init_stu_list(StudentList *head) {
    *head = (StudentNode*)malloc(sizeof(StudentNode));
    if (!*head) { perror("内存分配失败"); exit(1); }
    (*head)->next = NULL;
}

// 按学号查找学生
StudentNode* find_student_by_id_inner(StudentList head, const char *stu_id) {
    StudentNode *p = head->next;
    while (p) {
        if (strcmp(p->id, stu_id) == 0) return p;
        p = p->next;
    }
    return NULL;
}

// 新增学生
void add_student(StudentList head) {
    StudentNode *new_stu = (StudentNode*)malloc(sizeof(StudentNode));
    printf("请输入学生学号：");
    scanf("%s", new_stu->id);
    clear_input_buffer();
    
    // 检查学号是否重复
    if (find_student_by_id_inner(head, new_stu->id)) {
        printf("学号已存在！新增失败\n");
        free(new_stu);
        return;
    }
    
    printf("请输入学生姓名：");
    scanf("%s", new_stu->name);
    clear_input_buffer();
    new_stu->total_credit = 0;
    new_stu->course_ids[0] = '\0';  // 初始化选课记录为空
    new_stu->next = NULL;

    // 插入链表尾部
    StudentNode *p = head;
    while (p->next) p = p->next;
    p->next = new_stu;

    printf("新增学生成功！\n");
    char log[256];
    sprintf(log, "新增学生：%s %s", new_stu->id, new_stu->name);
    write_log(log);
    save_data(head, NULL);  // 保存学生数据
}

// 删除学生
void del_student(StudentList head) {
    char stu_id[20];
    printf("请输入要删除的学生学号：");
    scanf("%s", stu_id);
    clear_input_buffer();

    StudentNode *p = head->next, *prev = head;
    while (p) {
        if (strcmp(p->id, stu_id) == 0) {
            prev->next = p->next;
            free(p);
            printf("删除学生成功！\n");
            char log[100];
            sprintf(log, "删除学生：%s", stu_id);
            write_log(log);
            save_data(head, NULL);  // 保存学生数据
            return;
        }
        prev = p;
        p = p->next;
    }
    printf("学生不存在！删除失败\n");
}

// 修改学生姓名
void modify_student(StudentList head) {
    char stu_id[20], new_name[50];
    printf("请输入要修改的学生学号：");
    scanf("%s", stu_id);
    clear_input_buffer();

    StudentNode *stu = find_student_by_id_inner(head, stu_id);
    if (!stu) {
        printf("学生不存在！修改失败\n");
        return;
    }

    printf("当前姓名：%s，请输入新姓名：", stu->name);
    scanf("%s", new_name);
    clear_input_buffer();
    strcpy(stu->name, new_name);

    printf("修改姓名成功！\n");
    char log[256];
    sprintf(log, "修改学生姓名：学号%s，新姓名%s", stu_id, new_name);
    write_log(log);
    save_data(head, NULL);  // 保存学生数据
}

// 按学号查询学生详情
void query_student_by_id(StudentList s_head) {
    char id[20];
    printf("请输入要查询的学生学号：");
    scanf("%s", id);
    clear_input_buffer();

    StudentNode *stu = s_head->next;
    while (stu) {
        if (strcmp(stu->id, id) == 0) {
            printf("\n学生详情\n");
            printf("学号：%s\n", stu->id);
            printf("姓名：%s\n", stu->name);
            printf("总学分：%d\n", stu->total_credit);
            printf("已选课程：");
            if (strlen(stu->course_ids) == 0) {
                printf("无\n");
            } else {
                char temp[200];
                strcpy(temp, stu->course_ids);
                char *course_id = strtok(temp, ",");
                while (course_id) {
                    printf("%s ", course_id);
                    course_id = strtok(NULL, ",");
                }
                printf("\n");
            }
            return;
        }
        stu = stu->next;
    }
    printf("未找到该学生！\n");
}

// 按姓名查询
void query_student_by_name(StudentList head) {
    char name[50];
    printf("请输入要查询的学生姓名（模糊匹配）：");  
    scanf("%s", name);
    clear_input_buffer();

    StudentNode *p = head->next;
    int count = 0;
    printf("\n姓名包含'%s'的学生\n", name);  
    while (p) {
        if (strstr(p->name, name) != NULL) {  
            printf("学号：%s | 姓名：%s | 总学分：%d\n", 
                   p->id, p->name, p->total_credit);
            count++;
        }
        p = p->next;
    }
    if (count == 0) {
        printf("未找到姓名包含'%s'的学生\n", name);  
    }
    char log[256];
    sprintf(log, "按姓名模糊查询学生：%s", name);
    write_log(log);
}

// 查看所有学生
void show_all_students(StudentList head) {
    StudentNode *p = head->next;
    if (!p) {
        printf("暂无学生信息！\n");
        return;
    }

    printf("\n所有学生列表\n");
    while (p) {
        printf("学号：%s | 姓名：%s | 总学分：%d\n", p->id, p->name, p->total_credit);
        p = p->next;
    }
    write_log("查看所有学生列表");
}
